package com.example.anliku.service.serviceImpl;

import com.example.anliku.Utils.RedisUtil;
import com.example.anliku.entity.Comment;
import com.example.anliku.entity.Message;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.example.anliku.entity.Download;
import com.example.anliku.mapper.DownloadMapper;
import com.example.anliku.service.DownloadService;

import java.util.*;

@Service
public class DownloadServiceImpl implements DownloadService {

    @Resource
    private DownloadMapper downloadMapper;

    @Resource
    private RedisUtil redisUtil;

    @Override
    public Message deleteByPrimaryKey(Integer downloadId) {
        return downloadMapper.deleteByPrimaryKey(downloadId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insert(Download record) {
        return downloadMapper.insert(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(Download record) {
        record.setDownloadTime(new Date());
        boolean rs1 = redisUtil.hasKey("download" + record.getDownloadCaseId());
        boolean rs2 = redisUtil.hasKey("downloadAll");
        if (!rs1) {
            redisUtil.set("download" + record.getDownloadCaseId(), 1);
        } else {
            redisUtil.incr("download" + record.getDownloadCaseId(), 1);
        }
        if (!rs2) {
            redisUtil.set("downloadAll", 1);
        } else {
            redisUtil.incr("downloadAll", 1);
        }
        return downloadMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer downloadId) {
        return Message.success(null).add(downloadMapper.selectByPrimaryKey(downloadId));
    }

    @Override
    public Message updateByPrimaryKeySelective(Download record) {
        return downloadMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateByPrimaryKey(Download record) {
        return downloadMapper.updateByPrimaryKey(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectAll() {
        return Message.success(null).add(downloadMapper.selectAll());
    }

    @Override
    public Message chart() {
        List<Download> downloadList = downloadMapper.selectAll();
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < downloadList.size(); i++) {
            if (map.get(downloadList.get(i).getExample().getExampleName()) != null) {
                map.put(downloadList.get(i).getExample().getExampleName(), map.get(downloadList.get(i).getExample().getExampleName()) + 1);
            } else {
                map.put(downloadList.get(i).getExample().getExampleName(), 1);
            }

        }
        //得到map中所有的键
        Set<String> keyset = map.keySet();
        //创建set集合的迭代器
        Iterator<String> it = keyset.iterator();
        List<Map> mapList = new LinkedList<>();
        while (it.hasNext()) {
            HashMap maps = new HashMap();
            String key = it.next();
            Integer value = map.get(key);
            maps.put("value", value);
            maps.put("name", key);
            mapList.add(maps);
        }
        return Message.success(null).add(mapList);
    }

    @Override
    public Message selectByDownloadUserId(String downloadUserId) {
        return Message.success(null).add(downloadMapper.selectByDownloadUserId(downloadUserId));
    }

    @Override
    public Message selectByDownloadCaseId(String downloadCaseId) {
        return Message.success(null).add(downloadMapper.selectByDownloadCaseId(downloadCaseId));
    }

    @Override
    public List<Map> returnSum() {
        List<Download> downloadList = downloadMapper.selectAll();
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < downloadList.size(); i++) {
            if (map.get(downloadList.get(i).getDownloadCaseId()) != null) {
                map.put(downloadList.get(i).getDownloadCaseId(), map.get(downloadList.get(i).getDownloadCaseId()) + 1);
            } else {
                map.put(downloadList.get(i).getDownloadCaseId(), 1);
            }

        }
        //得到map中所有的键
        Set<String> keyset = map.keySet();
        //创建set集合的迭代器
        Iterator<String> it = keyset.iterator();
        List<Map> mapList = new LinkedList<>();
        while (it.hasNext()) {
            HashMap maps = new HashMap();
            String key = it.next();
            Integer value = map.get(key);
            maps.put("downloadCaseId", key);
            maps.put("downloadSum", value);
            mapList.add(maps);
        }
        return mapList;
    }


}


